<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: PageBase.php
 * Date: 2016/12/10
 * Time: 17:50
 */

namespace app\library;


use think\Cache;
use think\Cookie;
use think\Exception;
use think\Log;
use think\Request;
use think\Session;


class PageBase
{
    // ********* 返回数据 ************
    protected $errno;//返回值
    protected $errmsg;//返回信息
    protected $data;//返回数据

    // ********* 分页参数 ************
    protected $offset;
    protected $limit;

    protected $userinfo;
    protected $user_id;
    protected $user_name;
    protected $level;
    protected $phone;
    protected $label;
    protected $extend_info;
    protected $huanxin_username;

    public function __construct()
    {
        $this->initBase();
    }

    protected function initBase($param = null)
    {
        empty($param) && $param = Request::instance()->param();
        $this->errno = 0;
        $this->errmsg = '';
        $this->data = [];

        $this->offset = isset($param['offset']) ? $param['offset'] : 0;
        $this->limit = isset($param['limit']) ? $param['limit'] : 10;
    }

    protected function checkParam($param)
    {

    }

    protected function myExcute($param)
    {

    }

    public function excute($param)
    {
        try {
            $this->initBase($param);
            $this->myExcute($param);
            if ($this->errno) {
                throw new Exception("execute return error", $this->errno);
            }
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if ($err_code) {
                $this->errno = $err_code;
                if (Error::ERR_CUSTOM == $err_code) {
                    $this->errmsg = $e->getMessage();
                }
            } else if ($e->getMessage()) {
                $this->errno = \app\library\Error::ERR_SYS;
                $this->errmsg = $e->getMessage();
            }
            Log::error(sprintf("CLASS[%s] get exception err[%d] msg[%s] file[%s] line[%d] req[%s]", __CLASS__, $this->errno, $e->getMessage(), $e->getFile(), $e->getLine(), json_encode($param)));
        }
        return $this->getRes();
    }

    public function checkToken($param)
    {
        // 1、接口权限校验

        // 2、登录校验
        if (Cookie::has(CommonConfig::COOKIE_TYPE) && Cookie::has(CommonConfig::COOKIE_NAME)) {
            return true;
            $info = Cache::get(Cookie::get(CommonConfig::COOKIE_NAME));
            if (!empty($info)) {
                Cache::set(CommonConfig::COOKIE_NAME, $info, CommonConfig::TOKEN_EXPIRE_TIME);
                return true;
            } else {
                return false;
            }

        } else {
            if (!empty($param['token'])) {
                $info = Cache::get($param['token']);
                if (!empty($info)) {
                    Cache::set($param['token'], $info, CommonConfig::TOKEN_EXPIRE_TIME);
                    $this->userinfo = $info;
                    $this->user_id = $info['userid'];
                    $this->user_name = $info['user_name'];
                    $this->level = $info['level'];
                    $this->phone = $info['phone'];
                    $this->extend_info = json_decode($info['extend_info'], true);
                    $this->huanxin_username = $info['huanxin_username'];
                    $this->label = !empty($info['label']) ? json_decode($info['label'], true) : [];
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * @desc 验证签名
     * @param $param
     * @return bool
     */
    public function checkSign($param)
    {
        $sign = Common::computeSign($param);

        // 验证签名
        if ($sign != $param['sign']) {
            return false;
        }

        // 验证请求是否过期
        if (time() - $param['time'] > 600) {
            return false;
        }
        return true;
    }

    /**
     * 返回数据结构化
     *
     * @param int $errno
     * @param string $errmsg
     * @return array
     */
    protected function getRes($errno = 0, $errmsg = '')
    {
        if (!empty($errno)) {
            $this->errno = $errno;
        }
        if (!isset(\app\library\Error::$arr_err_msg[$this->errno])) {
            $this->errno = \app\library\Error::ERR_SYS;
        }
        if ('' != $errmsg) {
            $this->errmsg = $errmsg;
        } else if ('' == $this->errmsg) {
            $this->errmsg = \app\library\Error::$arr_err_msg[$this->errno];
        }
        //!is_array($this->data) && $this->data = [];
        $res = array(
            'errno' => $this->errno,
            'errmsg' => $this->errmsg,
            'data' => $this->data,
        );

        return $res;
    }

    public function modelToArray($param)
    {
        if (empty($param)) {
            return $param;
        }
        if ($param instanceof \think\Model) {
            return $param->getData();
        }

        if (is_array($param)) {
            $data = [];
            foreach ($param as $val) {
                //var_dump($val->getData());
                array_push($data, $val->getData());
            }
            return $data;
        }
    }
}
